import numpy as np
from scipy.stats import pearsonr


# 欧氏距离
class Euclidean():
    """
    https://bkimg.cdn.bcebos.com/formula/87a52feb423631405eb499ddaec6941d.svg
    """

    def __init(self):
        pass

    def similarity(self, vec1, vec2):
        distance = 0.0
        for i in range(len(vec1)):
            if vec1[i] == 0 or vec2[i] == 0:
                continue
            distance += (vec1[i] - vec2[i]) ** 2
        distance = np.sqrt(distance)
        return distance


# 皮尔逊
class Pearson():
    """
    https://bkimg.cdn.bcebos.com/formula/6451559c335fbb31eefc6273e2611d6a.svg
    """

    def __init(self):
        pass

    def similarity(self, vec1, vec2):
        return pearsonr(vec1, vec2)[0]


# 余弦
class Cosine():
    """
    https://bkimg.cdn.bcebos.com/formula/50c51a907a949e8bbdbfa9219ed8bd35.svg
    """

    def __init(self):
        pass

    def similarity(self, vec1, vec2):
        distance = 0.0
        product = 0.0
        vec1Len = 0.0
        vec2Len = 0.0
        for a, b in zip(vec1, vec2):
            product += a * b
            vec1Len += a ** 2
            vec2Len += b ** 2
        distance = product / np.sqrt(vec1Len * vec2Len)
        return distance
